﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using HJ212_Common;
using IPluginInterface;

namespace HJT212_Protocol_Server
{
    public class ServerPluginHandler
    {
        private PlugingManager PluginM;

        private log4net.ILog log = log4net.LogManager.GetLogger(
            System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public void Init()
        {
            try
            {
                #region 插件初始化
                var End = "SailHeroPlugin.dll";
                var interFace = typeof(IMessageReturn).FullName;
                PluginM = new PlugingManager(End, interFace);
                #endregion

            }
            catch (Exception ex)
            {

                log.Error("出现异常", ex);
            }
        }


        public void InvokeMessageRecived(MessageModel msg)
        {
            if (msg == null) return;

            foreach (IMessageReturn mreturn in PluginM.Plugins)
            {

                ParameterizedThreadStart Start = new ParameterizedThreadStart(o =>
                {
                    try { mreturn.OnMessageRecived(msg); }
                    catch (Exception ex) { log.Error("出现异常", ex); }
                });
                Thread Thrd = new Thread(Start);
                Thrd.Start();

            }

        }


        public void InvokeOffLineMessageRecived(MessageModel msg)
        {
            if (msg == null) return;
            foreach (IMessageReturn mreturn in PluginM.Plugins)
            {

                ParameterizedThreadStart Start =
                    new ParameterizedThreadStart(o =>
                                                    {
                                                        try { mreturn.OnOffLineMessageRecived(msg); }
                                                        catch (Exception ex) { log.Error("出现异常", ex); }
                                                    });
                Thread Thrd = new Thread(Start);
                Thrd.Start();

            }
        }

        public void InvokeOffLineMessageNote(string smstitle, string smscontent, string userid)
        {
            foreach (IMessageReturn mreturn in PluginM.Plugins)
            {

                ParameterizedThreadStart Start =
                    new ParameterizedThreadStart(o =>
                                                {
                                                    try { mreturn.OnOffLineMessageRecived(smstitle, smscontent, userid); }
                                                    catch (Exception ex) { log.Error("出现异常", ex); }
                                                });
                Thread Thrd = new Thread(Start);
                Thrd.Start();

            }

        }

        public void InvokeRunTimeMessageRecived(MessageModel msg)
        {
            if (msg == null) return;
            foreach (IMessageReturn mreturn in PluginM.Plugins)
            {

                ParameterizedThreadStart Start =
                    new ParameterizedThreadStart(o =>
                                                {
                                                    try { mreturn.OnRunTimeMessageRecived(msg); }
                                                    catch (Exception ex) { log.Error("出现异常", ex); }
                                                });
                Thread Thrd = new Thread(Start);
                Thrd.Start();

            }
        }

        public void InvokeHistoryMessageRecived(MessageModel msg)
        {
            if (msg == null) return;
            foreach (IMessageReturn mreturn in PluginM.Plugins)
            {

                ParameterizedThreadStart Start =
                    new ParameterizedThreadStart(o =>
                                                {
                                                    try
                                                    { mreturn.OnHistoryMessageRecived(msg); }
                                                    catch (Exception ex) { log.Error("出现异常", ex); }
                                                });
                Thread Thrd = new Thread(Start);
                Thrd.Start();

            }
        }


        public void InvokeOnAlertDesired(MessageModel msg)
        {
            if (msg == null) return;
            foreach (IMessageReturn mreturn in PluginM.Plugins)
            {

                ParameterizedThreadStart Start =
                    new ParameterizedThreadStart(o =>
                                                {
                                                    try { mreturn.OnAlertDesired(msg); }
                                                    catch (Exception ex) { log.Error("出现异常", ex); }
                                                });
                Thread Thrd = new Thread(Start);
                Thrd.Start();

            }
        }


    }
}
